//
//  SFFailureShowViewController.m
//  CAMTOOL
//
//  Created by 杨忠斌 on 11/14/16.
//  Copyright (c) 2016 sifo. All rights reserved.
//

#import "SFFailureShowViewController.h"
#import "SFCommonFounction.h"
#import "SFCommonCmd.h"
#import "SFTestSeqViewController.h"
@interface SFFailureShowViewController ()


@end

@implementation SFFailureShowViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
        NSBundle *bundle=[NSBundle mainBundle];
        NSString *plistPath=[bundle pathForResource:@"CAMPreferences" ofType:@"plist"];
        //获取属性列表文件中的全部数据
        NSDictionary *dict=[[NSDictionary alloc]initWithContentsOfFile:plistPath];
        self.event=dict;
    }
    return self;
}




-(IBAction)query:(id)sender{
    
    //  实例化并生成Command
    SFCommonCmd *ccmd=[[SFCommonCmd alloc]init];
    [ccmd setBarCodeValue:[_barCode stringValue]];
    NSString *wabisabi_ip=[self.event objectForKey:@"wabisabi_ip" ];
    NSString *wabisabi_port=[self.event objectForKey:@"wabisabi_port"];
    //127.0.0.1
    NSString *str=[@"curl " stringByAppendingFormat:@"%@%@%@%@",wabisabi_ip,@":",wabisabi_port,@"/api/units/"];
//    [ccmd setCommonStr:@"curl 172.18.8.56:8080/api/units/"];
    [ccmd setCommonStr:str];
    //    [ccmd setCommonStr:@"curl 127.0.0.1:8080/api/units/"];
    NSString *cmdStrSeq=[ccmd GenerateCommandwithSeq];
    NSLog(@"1st cmd StrSeq is %@",cmdStrSeq);
    NSArray *arguments = [NSArray arrayWithObjects: @"-c", cmdStrSeq, nil];
    //  运行Command返回字符串
    NSString *strSeq=[SFCommonFounction executeCmd:@"/bin/bash" withArguments:arguments];
    
    //  抓取最新的数据，并取出seqId，seqState
    NSArray *arr=[SFCommonFounction dataToJsonObject:strSeq];
    NSDictionary *dictionary=arr[arr.count-1];
    [self setSeqId:[dictionary objectForKey:@"id"]];
    [self setSeqState:[dictionary objectForKey:@"state"]];
    NSLog(@"%@\t%@",_seqId,_seqState);
    
    
    NSString *cmdStrAction=[ccmd GenerateCommandwithAction:_seqId];
    NSLog(@"2nd cmd StrAction is %@",cmdStrAction);
    NSArray *arguments2 = [NSArray arrayWithObjects: @"-c", cmdStrAction, nil];
    //  运行Command返回字符串
    NSString *strAction=[SFCommonFounction executeCmd:@"/bin/bash" withArguments:arguments2];
    
    NSArray *arr2=[SFCommonFounction dataToJsonObject:strAction];
    NSDictionary *dictionary2=arr2[arr2.count-1];
    [self setActionId:[dictionary2 objectForKey:@"id"]];
    [self setActionName:[dictionary2 objectForKey:@"name"]];
    [self setActionStatus:[dictionary2 objectForKey:@"status"]];
    NSLog(@"%@\t%@\t%@",_actionId,_actionName,_actionStatus);
    
    NSString *cmdStrResult=[ccmd GenerateCommandwithResult:_actionId];
    NSLog(@"3nd cmd StrResult is %@",cmdStrResult);
    NSArray *arguments3 = [NSArray arrayWithObjects: @"-c", cmdStrResult, nil];
    //  运行Command返回字符串
    NSString *strResult=[SFCommonFounction executeCmd:@"/bin/bash" withArguments:arguments3];
    
    
    NSArray *arr3=[SFCommonFounction dataToJsonObject:strResult];
    NSDictionary *dictionary3=arr3[arr3.count-1];
    [self setResultId:[dictionary3 objectForKey:@"id"]];
    [self setResultCode:[dictionary3 objectForKey:@"code"]];
    [self setResultStatus:[dictionary3 objectForKey:@"status"]];
    [self setResultDescription:[dictionary3 objectForKey:@"description"]];
    [self setResultInfo:[dictionary3 objectForKey:@"info"]];
    NSLog(@"%@\t%@\t%@\t%@\t%@",_resultId,_resultStatus,_resultDescription,_resultInfo,_resultCode );
    
    
    NSString *message=_actionName;
    if (![_resultDescription.className isEqual:@"NSNull"]) {
        message=[message stringByAppendingFormat:@"%@%@%@%@%@",@":",_resultDescription,@"(",_resultCode,@")"];
        NSLog(@"the message is : %@ %@",message,@"111");
    }else if(![_resultInfo.className isEqual:@"NSNull"])
    {
        message=[message stringByAppendingFormat:@"%@%@%@%@%@",@":",_resultInfo,@"(",_resultCode,@")"];
        NSLog(@"the message is : %@ %@",message,@"222");
    }else{
        message=[message stringByAppendingFormat:@"%@%@%@%@",@":No message given",@"(",_resultCode,@")"];
        NSLog(@"the message is : %@ : %@",message,@"333");
    }
//    NSLog(@"the message is : %@",message);
    
    [_feedBack setStringValue:message];

}
-(IBAction)clear:(id)sender
{
    [_barCode setStringValue:@""];
    [_feedBack setStringValue:@""];
}




@end
